<?php
session_start();

if (!isset($_SESSION['AdminId']) || $_SESSION['AdminId'] == "") {
    echo "<script type='text/javascript'>location.href='admin.php'</script>";
}

require_once 'common/config.php';
require_once 'common/conn.php';
require_once 'common/function.php';

require_once 'db/item.db.php';

$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$adjacents = isset($_GET['adjacents']) ? intval($_GET['adjacents']) : 0;

$type = isset($_GET['type']) ? trim($_GET['type']) : "";

if ($page <= 0) {
    $page = 1;
}
if ($adjacents <= 0) {
    $adjacents = 4;
}


$limit = 8;
$start = ($page - 1) * $limit;

$dbc = new DBC();
$itemDb = new ItemDb($dbc);
$array = $itemDb->getItem($type, $start, $limit);
$numberOfObject = $itemDb->getItemNumber($type);

$dbc->close();
?>
<!DOCTYPE html>
<html>
    <head>
        <title>已审核老师列表</title>
        <meta charset="UTF-8">
        <link href="<?php echo $flatUi; ?>dist/css/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
        <script src="<?php echo $flatUi; ?>js/jquery-1.11.3.js"></script>
        <script type="text/javascript">
            $().ready(function () {
                btnClickHandler();
            });

            function btnClickHandler() {
                $(".btn").click(function () {
                    var idStr = $(this).attr("id");
                    if (idStr.indexOf("edit_") != -1) {
                        var id = idStr.replace("edit_", "");
                        location.href = "item_edit.php?id=" + id;
                    } else if (idStr.indexOf("disable_") != -1) {
                        var id = idStr.replace("disable_", "");
                        location.href = "../handle/teacher.handle.php?userId=" + id + "&action=disable&status=2";
                    } else if (idStr.indexOf("delete_") != -1) {
                        var id = idStr.replace("delete_", "");
                        if (confirm("确定删除" + $(this).attr("name") + "?")) {
                            location.href = "action/item.action.php?action=delete&idx=" + id;
                        }
                    } else if (idStr.indexOf("create") != -1) {
                        location.href = "item_edit.php";
                    }
                });
            }

        </script>
    </head>
    <body>
        <div style="text-align: right"><input id="create" type="button" class="btn btn-default" value="增加商品"/></div>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>序号</th>
                    <th>编号</th>
                    <th>名称</th>
                    <th>价格</th>
                    <th>类型</th>
                    <th>图片</th>
                    <th>编辑</th>
                    <th>删除</th>
                </tr>
            </thead>
            <tbody id="tbody">
                <?php
                for ($i = 0; $i < count($array); $i++) {
                    $j = $i + 1;
                    $itemInfo = $array[$i];
                    $itemName = $itemInfo['ItemName'];
                    $image = $itemInfo['ImageUrl'] == "" ? "" : "<img src='" . $itemInfo['ImageUrl'] . "' width='100px' height='100px'></img>";
                    ?>
                    <tr>
                        <td><?php echo $j; ?></td>
                        <td><?php echo $itemInfo['ItemNo']; ?></td>
                        <td><?php echo $itemName; ?></td>
                        <td><?php echo $itemInfo['Price']; ?></td>
                        <td><?php echo $itemInfo['Type']; ?></td>
                        <td><?php echo $image; ?></td>
                        <td><input id="edit_<?php echo $itemInfo['id_x']; ?>" type="button" class="btn btn-primary" value="编辑"/></td>
                        <td><input id="delete_<?php echo $itemInfo['id_x']; ?>" name="<?php echo $itemName; ?>" type="button" class="btn btn-danger" value="删除"/></td>
                    </tr>
                <?php } ?>
                <tr id="bottom_tr"></tr>
            </tbody>
        </table>
        <div style="width: 100%; text-align: center; padding-bottom: 20px;">
            <?php
            $tpages = intval($numberOfObject / $limit) + (intval($numberOfObject % $limit) == 0 ? 0 : 1);
            $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages . "&amp;adjacents=" . $adjacents;
            echo paginate_three($reload, $page, $tpages, $adjacents);
            ?></div>
    </body>
</html>